﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.Note;
using Enterprise.Service.Note;
using Enterprise.Control;
namespace Enterprise.UI.Web.Modules.Notice
{
    public partial class Manage : Enterprise.Service.BasePage
    {
        int Id = (int)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string Cmd = (string)Utility.sink("Cmd", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            OnBindData();
        }

        private void OnBindData()
        {
            NoteService nService = new NoteService();
            var q = nService.NoteDisp(Id);
            if (q != null)
            {
                if (Cmd == "Edit")
                {
                    tb_Title.Text = q.NTITLE;
                    tb_Content.Text = q.NREMARK;
                    ListItem li = tb_Remind.Items.FindByValue(q.NISREMIND.ToString());
                    if (li != null)
                        li.Selected = true;
                    if (q.NISREMIND == 1)
                    {
                        tb_RemindTime.Text = q.NREMINDTIME.ToShortDateString();
                    }
                }
                else if (Cmd == "Delete")
                {
                    if (q.NUSERID == Utility.Get_UserId)
                    {
                        string _note = "操作成功";
                        q.DB_Option_Action = "Delete";
                        try
                        {
                            nService.NoteExecute(q);
                        }
                        catch (Exception ex)
                        {
                            _note = ex.Message;
                        }
                        Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Notices.aspx?dt="+q.NCREATETIME.ToShortDateString());
                    }
                }
            }
        }

        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
            string _note = "操作成功";
            try
            {
                NoteService nService = new NoteService();
                NoteEntity entity = new NoteEntity();
                if (string.IsNullOrEmpty(Cmd))
                {
                    entity.DB_Option_Action = "Insert";
                    entity.NUSERID = Utility.Get_UserId;
                    entity.NCREATETIME = DateTime.Now;
                }
                else if (Cmd == "Edit")
                {
                    entity.DB_Option_Action = "Update";
                    entity = nService.NoteDisp(Id);
                }
                entity.NTITLE = (string)Utility.sink(tb_Title.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.NREMARK = (string)Utility.sink(tb_Content.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.NISREMIND = (int)Utility.sink(tb_Remind.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
                entity.NREMINDTIME = (DateTime)Utility.sink(tb_RemindTime.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Date);
                if (entity.NISREMIND == 1 && entity.NREMINDTIME == DateTime.MaxValue)
                {
                    Utility.ShowMsg(this.Page, "如果备忘需要提醒，请设置提醒时间");
                }
                else
                {
                    nService.NoteExecute(entity);
                }
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
            Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Index.aspx");
        }

    }
}